package testrj.tools;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

import testrj.domain.MethodToTest;
import testrj.util.ReflectionUtils;

public class MethodTestCaseGenerator {
	
	public void generateStubFor(Class<?> clazz, Method method) {
		Class<?>[] inputParamClasses = method.getParameterTypes();
		List<Object> paramObjects = new ArrayList<Object>();
		for(Class<?> paramClass: inputParamClasses) {
			paramObjects.add(ReflectionUtils.getInstanceOfClassForcibly(paramClass));
		}
		Object thisObject = ReflectionUtils.getInstanceOfClassForcibly(clazz);
		Object returnObject = ReflectionUtils.getInstanceOfClassForcibly(method.getReturnType());
		MethodToTest mtt = MethodToTest.init(thisObject.getClass().getName(), method.getName(), thisObject, paramObjects.toArray());
		mtt.recordReturnValueAndPersist(returnObject);
	}
	
	public void generateStubFor(Class<?> clazz) {
		for(Method method: clazz.getDeclaredMethods()) {
			generateStubFor(clazz, method);
		}
	}
	
	public void generateStubFor(Class<?> clazz, String methodName) {
		for(Method method: clazz.getDeclaredMethods()) {
			if(method.getName().trim().equals(methodName)) {
				generateStubFor(clazz, method);
			}
		}
	}
	
	public void generateStubFor(String className, String methodName) {
		generateStubFor(ReflectionUtils.getClassForClassName(className), methodName);
	}
	
	public void generateStubFor(String className) {
		generateStubFor(ReflectionUtils.getClassForClassName(className));
	}
	
	public static MethodTestCaseGenerator newInstance() {
		return new MethodTestCaseGenerator();
	}
	
	private MethodTestCaseGenerator() {}
	
	public static void main(String[] args) {
		MethodTestCaseGenerator.newInstance().generateStubFor("testrj.domain.DomainObject", "isSymbolicPrimitive");
//		MethodTestCaseGenerator.newInstance().generateStubFor("testrj.domain.MethodToTest");
//		MethodTestCaseGenerator.newInstance().generateStubFor("testrj.domain.DomainObject");
//		MethodTestCaseGenerator.newInstance().generateStubFor("testrj.domain.SerializedData");
//		MethodTestCaseGenerator.newInstance().generateStubFor("testrj.domain.TestCase");
	}
}